package com.project.forum.controller;

import com.project.forum.common.AppResult;
import com.project.forum.common.ResultCode;
import com.project.forum.config.AppConfig;
import com.project.forum.model.Message;
import com.project.forum.model.User;
import com.project.forum.services.IMessageService;
import com.project.forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Time:22:52
 */
@Slf4j
@Api(tags = "站内信接口")
@RequestMapping("/message")
@RestController
public class MessageController {
    @Resource
    private IMessageService messageService;
    @Resource
    private IUserService userService;
    @ApiOperation("发送站内信")
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @ApiParam("接受者id") @RequestParam("receiveUserId") @NonNull Long receiveUserId,
                          @ApiParam("内容") @RequestParam("content") @NonNull String content){
        //获取当前登录的用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //当前登录的用户状态,如果是禁言状态，不能发站内信，返回错误信息
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //不能给自己发站内信
        if (user.getId()==receiveUserId){
            //返回错误信息
            return AppResult.failed("不能给自己发送站内信");
        }
        //校验接受者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser==null||receiveUser.getDeleteState()==1){
            //接受者状态不正常
            return AppResult.failed("接受者状态异常");
        }
        //封装对象
        Message message=new Message();
        message.setPostUserId(user.getId());//发送者id
        message.setReceiveUserId(receiveUserId);//接收者id
        message.setContent(content);//站内信内容
        //调用service
        messageService.create(message);
        //返回结果
        return AppResult.success("发送成功");
    }

    @ApiOperation("获取未读数")
    @GetMapping("getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        //获取当前登录的用户
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用Service
        Integer count = messageService.selectUnreadCount(user.getId());//当前登录用户的id
        //返回结果
        return AppResult.success(count);

    }

    @ApiOperation("查询用户的所有站内信")
    @GetMapping("getAll")
    public AppResult<List<Message>> getAll (HttpServletRequest request){
        //获取当前登录的用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用Service
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        //返回结果
        return AppResult.success(messages);
    }

    @ApiOperation("更新为已读")
    @PostMapping("/markRead")
    public AppResult markRead (HttpServletRequest request,
                               @ApiParam("站内信Id") @RequestParam("id") @NonNull Long id){
        //根据id查询站内信
        Message message = messageService.selectById(id);
        //判断站内信是否存在
        if (message==null||message.getDeleteState()==1){
            //返回错误
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //站内信是不是自己的
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getId()!=message.getReceiveUserId()){
            //不是自己的
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //调用Service，把状态更新为已读
        messageService.updateStateById(id, (byte) 1);
        //返回结果
        return AppResult.success();
    }

    @ApiOperation("回复站内信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @ApiParam("要回复的站内信id") @RequestParam("repliedId") @NonNull Long repliedId,
                           @ApiParam("站内信的内容") @RequestParam("content") @NonNull String content){
        //校验当前用户的登录状态
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //校验要回复的站内信状态
        Message existsMessage = messageService.selectById(repliedId);
        if (existsMessage==null||existsMessage.getDeleteState()==1){
            //返回错误
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //不能给自己回复
        if (user.getId()==existsMessage.getPostUserId()){
            //返回错误结果
            return AppResult.failed("不能给自己的站内信回复");
        }
        //构造对象
        Message message=new Message();
        message.setPostUserId(user.getId());//发送者
        message.setReceiveUserId(existsMessage.getPostUserId());//接受者
        message.setContent(content);
        //调用Service
        messageService.reply(repliedId,message);
        //返回结果
        return AppResult.success();
    }
}
